package com.boco.blch.dispatcher.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.boco.blch.dispatcher.service.HtmlReportService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.boco.blch.dispatcher.params.SingleLoginParams;
import com.boco.blch.dispatcher.po.compliant;
import com.boco.blch.dispatcher.service.BigDataService;
import com.boco.blch.runner.service.ShellExecuteService;

@RestController
public class BigDataController {
	@Autowired
	private BigDataService bigDataService;
	@Autowired
	private HtmlReportService htmlReportService;
	private Logger logger = Logger.getLogger(BigDataController.class);
	/*
	 * @RequestMapping("/BOCO_Count") public int getCountByName(String
	 * compliantName, String plantform){
	 * 
	 * int count = bigDataService.getCountByName(compliantName, plantform);
	 * return count; }
	 */

	@RequestMapping("/BOCO_ExecuteByName")
	public void execByCompliantName(String DeviceId, String Ip, String Port, String User, String UserEncryptd,
			String Password, String PasswordEncryptd, String compliantName, String plantform, String nodeType,
			String taskNo) {
		List<compliant> compliantList = bigDataService.getCompliantByCompName(compliantName, plantform, nodeType);
		if (compliantList != null && compliantList.size() != 0) {
			for (compliant compliant : compliantList) {
				bigDataService.compliantService(DeviceId, Ip, Port, User, UserEncryptd, Password, PasswordEncryptd,
						compliant, taskNo);
			}
		}
	}

	@RequestMapping("/exec_on_single_login")
	public void execOnSingleLogin(SingleLoginParams slp, HttpServletRequest request, HttpServletResponse response) {
		logger.info("接受到请求,受检设备ID: " + slp.getDeviceId() + " ,受检组件: " + slp.getComponentIds() + " ,任务编号:"
				+ slp.getTaskNo());
		bigDataService.execOnSingleLogin(slp);
	}

	@RequestMapping("/connect_test")
	public String connectTest(String Ip, Integer Port, String User, String Password) {
		return ShellExecuteService.connectTest(Ip, Port, User, Password);
	}

	@RequestMapping("/hotkey_check")
	public void hotKeyCheck(String ip) {
		logger.info("接收到一键检查请求，IP = " + ip);
		bigDataService.hotkeyCheck(ip);
	}

	@RequestMapping("/BOCO_Baseline_Scan")
	public void baselineScan(String ip, String component,HttpServletResponse response) throws IOException {
		bigDataService.baselineScan(ip, component,response);
	}

	@RequestMapping("/BOCO_Baseline_Status")
	public String baselineStatus(String taskNo) {
		return bigDataService.balineStatus(taskNo);
	}
	
	@RequestMapping(value = "/BOCO_Baseline_Result", method = RequestMethod.POST)
	public String baselineResult( String taskNo){
		logger.info("gen xml report for : " + taskNo);
		return bigDataService.baselineResult(taskNo);
	}

	@RequestMapping("/BOCO_Baseline_HtmlResult")
	public void baselineHtmlResult(String taskNo,HttpServletResponse response){
		logger.info("gen zip report for : " + taskNo);
		htmlReportService.HtmlReport(taskNo,response);
	}
}
